import type { NextFunction, Request, Response } from 'express';
import { AppDataSource } from '@/data-source';
import { Role } from '@/entity/Role';
import {
  responseSuccess,
  responseError,
} from '@/share/interface/standard';
const bcrypt = require('bcryptjs');

export class RoleController {
  private roleRepository = AppDataSource.getRepository(Role);

  async get(
    request: Request,
    response: Response,
    next: NextFunction
  ) {
    const res = await this.roleRepository.find();
    return responseSuccess(res);
  }

  // 完成任务 or 签到，获取盒饭
  async addBento(request: Request, response: Response) {
    const { id, bento = 0 } = request.body;
    const role = await this.roleRepository.findOne({
      where: { id },
    });

    if (!role) {
      return responseError('角色不存在');
    }
    role.bento += bento;
    await this.roleRepository.save(role);
    return responseSuccess('添加了' + bento + '个便当');
  }
}
